package com.wjc.wjc.web.admin;


import com.wjc.wjc.service.StudentService;
import com.wjc.wjc.unity.Record;
import com.wjc.wjc.unity.Student;
import com.wjc.wjc.unity.StudentQury;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

@Controller
public class InforController {


    @Autowired
    private StudentService studentService;

    @GetMapping("/admin/infor")
    public String record(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                         StudentQury student,Model model){
        model.addAttribute("page",studentService.listStudent(pageable,student));
        return "admin/infor";
    }


    @PostMapping("/infor/search")
    public String search(@PageableDefault(size = 5,sort = {"id"},direction = Sort.Direction.DESC) Pageable pageable,
                         StudentQury student,Model model) {
        model.addAttribute("page", studentService.listStudent(pageable, student));
        return "/admin/infor :: inf";
    }

    @GetMapping("/infor/input")
    public String input(){
        return "admin/infor-input";
    }



    @GetMapping("/infor/{id}/resetpwd")
    public String resetPwd(@PathVariable Long id){
        studentService.resetPassword(id);
        return "redirect:/admin/infor";
    }

    @PostMapping("/infor/save")
    public String save(Student student, RedirectAttributes attributes){
        Student s=studentService.save(student);
        if (s!=null){
            return "redirect:/admin/infor";
        }
        attributes.addFlashAttribute("message","保存失败");
        return "redirect:/infor/input";
    }

    @GetMapping("/infor/{id}/delete")
    public String delete(@PathVariable Long id){
        studentService.delete(id);
        return "redirect:/admin/infor";
    }
}
